#!/usr/bin/env python
import sys, string, random

outputSize=1000000;

fi = open("/Volumes/Data/switch_project/exp16/1000M/900M/1/probability_loss_delay_episode.txt", "r")
fo = open("/Volumes/Data/switch_project/exp16/1000M/900M/1/generated_output.txt", "w");

#read probability
line = fi.readline();
pr_loss_given_delay = string.split(line);
line = fi.readline();
pr_delay_given_loss = string.split(line);

pr_interval_loss_given_delay=[0]*len(pr_loss_given_delay);
pr_interval_delay_given_loss=[0]*len(pr_delay_given_loss);

#generate corresponding interval for probability
pr_interval_loss_given_delay[0] = float(pr_loss_given_delay[0]); 
for i in range(1, len(pr_loss_given_delay)):
	pr_interval_loss_given_delay[i] = pr_interval_loss_given_delay[i-1]+float(pr_loss_given_delay[i]); 

pr_interval_delay_given_loss[0] = float(pr_delay_given_loss[0]); 
for i in range(1, len(pr_delay_given_loss)):
	pr_interval_delay_given_loss[i] = pr_interval_delay_given_loss[i-1]+float(pr_delay_given_loss[i]);

print "pr_loss_given_delay", pr_loss_given_delay, "\n";
print "pr_delay_given_loss", pr_delay_given_loss, "\n";
print "pr_interval_loss_given_delay", pr_interval_loss_given_delay, "\n";
print "pr_interval_delay_given_loss", pr_interval_delay_given_loss, "\n";

#random number preparation
for i in range(10):
	temp_rn=random.uniform(0,1);
	#print temp_rn; 

#run
#initial condition, same as experiment, not necessary I guess
cur_episode = 5;
is_cur_loss = 1; #flag to indiciate if the current episode is loss or delay

fo.write("-");
fo.write(str(cur_episode));
fo.write("\n");

for i in range(1, outputSize):
	rn = random.uniform(0,1);
	if(is_cur_loss == 1): #cur episode is loss, next is delay, look at pr_delay_given_loss
		is_cur_loss = 0;
		for j in range(len(pr_interval_delay_given_loss)):
			if(rn <= pr_interval_delay_given_loss[j]):
				cur_episode = j;
				fo.write(str(cur_episode));
				fo.write("\n");
				break;
	elif(is_cur_loss == 0): #cur episode is delay, next is loss, look at pr_loss_given_delay
		is_cur_loss = 1;
		for j in range(len(pr_interval_loss_given_delay)):
			if(rn <= pr_interval_loss_given_delay[j]):
				cur_episode = j;
				fo.write("-");
				fo.write(str(cur_episode));
				fo.write("\n");
				break;

fi.close();
fo.close();
			







